﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLFacade.entities;
using BLFacade;

namespace ProjectUI
{
    public partial class ProjectForm : Form
    {
        private IProject project;
        public ProjectForm(ICustomer customer)
        {
            InitializeComponent();
            cmbxCustomer.DataSource = FacadeHolder.DataFacade.GetCustomers();
            cmbxCustomer.SelectedItem = customer;
            cmbxProjectManager.DataSource = FacadeHolder.DataFacade.GetEmployees();
            //cmbxProjectManager.SelectedItem = cmbxProjectManager.DataSource;
            //cmbxProjectManager.SelectedValue = project.ProjectManager.FullName;
            btnStartClose.Enabled = false;
            btnEdit.Visible = false;
            btnStartClose.Visible = false;
            btnTasks.Visible = false;
            lblStartingDate.Visible = false;
            lblCreatingDateValue.Visible = false;
            lblClosingDateValue.Visible = false;
            lblEndingDate.Visible = false;
            if (FacadeHolder.UserFacade.IsAdministrator())
            {
                cmbxCustomer.Enabled = true;
                cmbxProjectManager.Enabled = true;
            }
            this.Text = String.Format("New project for {0}", customer.Name);
        }

        public ProjectForm(IProject project)
        {
            InitializeComponent();
            grbxInfo.Enabled = false;
            this.project = project;
            txtName.Text = project.Name;
            txtDescription.Text = project.Description;
            dateDeadline.Value = project.DeadLine;
            if (project.StartDate.HasValue) lblCreatingDateValue.Text = project.StartDate.Value.ToShortDateString();
            else
            {
                lblStartingDate.Visible = false;
                lblCreatingDateValue.Visible = false;
                btnStartClose.Text = "Start";
                if (!FacadeHolder.UserFacade.CurrentPerson.Equals(project.ProjectManager)) btnStartClose.Visible = false;
            }
            if (project.EndDate.HasValue) lblClosingDateValue.Text = project.EndDate.Value.ToShortDateString();
            else
            {
                grbxInfo.Controls.Remove(lblClosingDateValue);
                grbxInfo.Controls.Remove(lblEndingDate);
                btnStartClose.Text = "Close";
                if (!FacadeHolder.UserFacade.IsAdministrator()) flowPanelButtons.Controls.Remove(btnStartClose);
            }
            cmbxCustomer.DataSource = FacadeHolder.DataFacade.GetCustomers();
            cmbxCustomer.SelectedItem = project.Customer;
            cmbxProjectManager.DataSource = FacadeHolder.DataFacade.GetEmployees();
            cmbxProjectManager.SelectedValue = project.ProjectManager.FullName;
            if (FacadeHolder.UserFacade.IsAdministrator())
            {
                cmbxCustomer.Enabled = true;
                cmbxProjectManager.Enabled = true;
                btnTasks.Visible = false;
            }
            else if (!FacadeHolder.UserFacade.CurrentPerson.Equals(project.ProjectManager))
            {
                flowPanelButtons.Controls.Remove(btnEdit);
                flowPanelButtons.Visible = false;
            }
            btnCreate.Text = "Submit";
            btnCreate.Enabled = false;
            this.Text = String.Format("Information about {0} project", project.Name);
        }


        private void btnEdit_Click(object sender, EventArgs e)
        {
            btnCreate.Enabled = grbxInfo.Enabled = !grbxInfo.Enabled;
        }

        private void txtName_Validating(object sender, CancelEventArgs e)
        {
            if (String.IsNullOrWhiteSpace(txtName.Text))
            {
                errorFormProvider.SetError(txtName, "This field mustn't be empty");
            }
            else
            {
                errorFormProvider.SetError(txtName, "");
            }
        }

        private void dateDeadline_Validating(object sender, CancelEventArgs e)
        {
            if (dateDeadline.Value < DateTime.Now)
            {
                errorFormProvider.SetError(dateDeadline, "Incorrect deadline");
            }
            else
            {
                errorFormProvider.SetError(dateDeadline, "");
            }
        }

        private bool IsValid()
        {
            foreach (Control c in grbxInfo.Controls)
            {
                if (!String.IsNullOrEmpty(errorFormProvider.GetError(c))) return false;
            }
            return true;
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            if (!IsValid()) return;
            if (project != null)
            {
                project.Name = txtName.Text;
                project.Description = txtDescription.Text;
                project.DeadLine = dateDeadline.Value;
                project.Customer = (ICustomer)cmbxCustomer.SelectedItem;
                project.ProjectManager = (IEmployee)cmbxProjectManager.SelectedItem;
            }
            else ((ICustomer)cmbxCustomer.SelectedItem).CreateProject(txtName.Text, txtDescription.Text, dateDeadline.Value, ((IEmployee)cmbxProjectManager.SelectedItem));
            FacadeHolder.DataFacade.SaveChanges();
            this.Close();
        }

        private void btnTasks_Click(object sender, EventArgs e)
        {
            TaskListForm tasks = new TaskListForm(project);
            tasks.MdiParent = this.MdiParent;
            tasks.Show();
        }

        private void btnStartClose_Click(object sender, EventArgs e)
        {
            if (project != null)
            {
                if (project.StartDate.HasValue) project.Close();
                else project.Start();
            }
            FacadeHolder.DataFacade.SaveChanges();
            this.Close();
        }
    }
}
